# Makefile for Advanced Crypto System
# 高级加密解密系统编译文件

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g -O2 -D_GNU_SOURCE
LDFLAGS = 

# 目录设置
SRCDIR = .
OBJDIR = obj
BINDIR = bin

# 源文件
COMMON_SRC = crypto_common.c
ENCRYPT_SRC = encrypt.c
DECRYPT_SRC = decrypt.c
KEYGEN_SRC = keygen.c
VERIFY_SRC = verify_crypto.c
SIMPLE_TEST_SRC = simple_test.c
STEP_TEST_SRC = step_by_step_test.c
MINIMAL_TEST_SRC = minimal_test.c
DEBUG_FULL_SRC = debug_full_test.c
FILE_PROCESS_SRC = file_process_test.c

# 目标文件
COMMON_OBJ = $(OBJDIR)/crypto_common.o
ENCRYPT_OBJ = $(OBJDIR)/encrypt.o
DECRYPT_OBJ = $(OBJDIR)/decrypt.o
KEYGEN_OBJ = $(OBJDIR)/keygen.o
VERIFY_OBJ = $(OBJDIR)/verify_crypto.o
SIMPLE_TEST_OBJ = $(OBJDIR)/simple_test.o
STEP_TEST_OBJ = $(OBJDIR)/step_by_step_test.o
MINIMAL_TEST_OBJ = $(OBJDIR)/minimal_test.o
DEBUG_FULL_OBJ = $(OBJDIR)/debug_full_test.o
FILE_PROCESS_OBJ = $(OBJDIR)/file_process_test.o

# 可执行文件
ENCRYPT_TARGET = $(BINDIR)/encrypt
DECRYPT_TARGET = $(BINDIR)/decrypt
KEYGEN_TARGET = $(BINDIR)/keygen
VERIFY_TARGET = $(BINDIR)/verify_crypto
SIMPLE_TEST_TARGET = $(BINDIR)/simple_test
STEP_TEST_TARGET = $(BINDIR)/step_test
MINIMAL_TEST_TARGET = $(BINDIR)/minimal_test
DEBUG_FULL_TARGET = $(BINDIR)/debug_full
FILE_PROCESS_TARGET = $(BINDIR)/file_process

# 默认目标
all: $(ENCRYPT_TARGET) $(DECRYPT_TARGET) $(KEYGEN_TARGET) $(VERIFY_TARGET)

# 创建目录
$(OBJDIR):
	mkdir -p $(OBJDIR)

$(BINDIR):
	mkdir -p $(BINDIR)

# 编译公共对象文件
$(COMMON_OBJ): $(COMMON_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(COMMON_SRC) -o $@

# 编译加密对象文件
$(ENCRYPT_OBJ): $(ENCRYPT_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(ENCRYPT_SRC) -o $@

# 编译解密对象文件
$(DECRYPT_OBJ): $(DECRYPT_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(DECRYPT_SRC) -o $@

# 编译密钥生成对象文件
$(KEYGEN_OBJ): $(KEYGEN_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(KEYGEN_SRC) -o $@

# 编译验证对象文件
$(VERIFY_OBJ): $(VERIFY_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(VERIFY_SRC) -o $@

# 编译简单测试对象文件
$(SIMPLE_TEST_OBJ): $(SIMPLE_TEST_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(SIMPLE_TEST_SRC) -o $@

# 编译逐步测试对象文件
$(STEP_TEST_OBJ): $(STEP_TEST_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(STEP_TEST_SRC) -o $@

# 编译最简测试对象文件
$(MINIMAL_TEST_OBJ): $(MINIMAL_TEST_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(MINIMAL_TEST_SRC) -o $@

# 编译完整调试对象文件
$(DEBUG_FULL_OBJ): $(DEBUG_FULL_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(DEBUG_FULL_SRC) -o $@

# 编译文件处理测试对象文件
$(FILE_PROCESS_OBJ): $(FILE_PROCESS_SRC) crypto_common.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $(FILE_PROCESS_SRC) -o $@

# 链接加密程序
$(ENCRYPT_TARGET): $(ENCRYPT_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(ENCRYPT_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接解密程序
$(DECRYPT_TARGET): $(DECRYPT_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(DECRYPT_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接密钥生成程序
$(KEYGEN_TARGET): $(KEYGEN_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(KEYGEN_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接验证程序
$(VERIFY_TARGET): $(VERIFY_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(VERIFY_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接简单测试程序
$(SIMPLE_TEST_TARGET): $(SIMPLE_TEST_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(SIMPLE_TEST_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接逐步测试程序
$(STEP_TEST_TARGET): $(STEP_TEST_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(STEP_TEST_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接最简测试程序
$(MINIMAL_TEST_TARGET): $(MINIMAL_TEST_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(MINIMAL_TEST_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接完整调试程序
$(DEBUG_FULL_TARGET): $(DEBUG_FULL_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(DEBUG_FULL_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 链接文件处理测试程序
$(FILE_PROCESS_TARGET): $(FILE_PROCESS_OBJ) $(COMMON_OBJ) | $(BINDIR)
	$(CC) $(FILE_PROCESS_OBJ) $(COMMON_OBJ) $(LDFLAGS) -o $@

# 只编译加密程序
encrypt: $(ENCRYPT_TARGET)

# 只编译解密程序
decrypt: $(DECRYPT_TARGET)

# 只编译密钥生成程序
keygen: $(KEYGEN_TARGET)

# 只编译验证程序
verify: $(VERIFY_TARGET)

# 只编译简单测试程序
simple: $(SIMPLE_TEST_TARGET)

# 只编译逐步测试程序
step: $(STEP_TEST_TARGET)

# 只编译最简测试程序
minimal: $(MINIMAL_TEST_TARGET)

# 只编译完整调试程序
debug: $(DEBUG_FULL_TARGET)

# 只编译文件处理测试程序
filetest: $(FILE_PROCESS_TARGET)

# 测试程序
test: $(ENCRYPT_TARGET) $(DECRYPT_TARGET)
	@echo "创建测试文件..."
	@echo "这是一个测试文件，用于验证加密解密功能。" > test.txt
	@echo "测试加密..."
	@./$(ENCRYPT_TARGET) < test_encrypt_input.txt || echo "请手动运行加密测试"
	@echo "测试完成。请手动验证加密解密功能。"

# 清理编译文件
clean:
	rm -rf $(OBJDIR) $(BINDIR)
	rm -f test.txt test_encrypted.bin test_decrypted.txt *.key

# 重新编译
rebuild: clean all

# 安装程序
install: $(ENCRYPT_TARGET) $(DECRYPT_TARGET) $(KEYGEN_TARGET)
	cp $(ENCRYPT_TARGET) /usr/local/bin/advanced_encrypt
	cp $(DECRYPT_TARGET) /usr/local/bin/advanced_decrypt
	cp $(KEYGEN_TARGET) /usr/local/bin/advanced_keygen
	chmod +x /usr/local/bin/advanced_encrypt
	chmod +x /usr/local/bin/advanced_decrypt
	chmod +x /usr/local/bin/advanced_keygen

# 卸载程序
uninstall:
	rm -f /usr/local/bin/advanced_encrypt
	rm -f /usr/local/bin/advanced_decrypt
	rm -f /usr/local/bin/advanced_keygen

# 创建发布包
release: clean all
	mkdir -p release
	cp $(ENCRYPT_TARGET) $(DECRYPT_TARGET) $(KEYGEN_TARGET) release/
	cp *.h *.c Makefile README.md release/
	tar -czf advanced_crypto_system.tar.gz release/
	rm -rf release/

# 显示帮助
help:
	@echo "可用的 make 目标:"
	@echo "  all        - 编译加密、解密和密钥生成程序 (默认)"
	@echo "  encrypt    - 只编译加密程序"
	@echo "  decrypt    - 只编译解密程序"
	@echo "  keygen     - 只编译密钥生成程序"
	@echo "  test       - 编译并运行基本测试"
	@echo "  clean      - 清理编译文件"
	@echo "  rebuild    - 重新编译"
	@echo "  install    - 安装程序到系统"
	@echo "  uninstall  - 从系统卸载程序"
	@echo "  release    - 创建发布包"
	@echo "  help       - 显示此帮助信息"

# 显示编译信息
info:
	@echo "编译器: $(CC)"
	@echo "编译选项: $(CFLAGS)"
	@echo "源文件: $(COMMON_SRC) $(ENCRYPT_SRC) $(DECRYPT_SRC) $(KEYGEN_SRC)"
	@echo "目标文件: $(ENCRYPT_TARGET) $(DECRYPT_TARGET) $(KEYGEN_TARGET)"

# 声明伪目标
.PHONY: all encrypt decrypt keygen test clean rebuild install uninstall release help info